/* rcinvcon.i - Checks whether invoice for SO's/abs's can consolidated        */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.4.1.2 $                                                           */
/*V8:ConvertMode=NoConvert                                                    */
/* REVISION: 8.6    LAST MODIFIED: 07/22/96   BY: *K003* Vinay Nayak-Sujir    */
/* REVISION: 8.6    LAST MODIFIED: 12/30/97   BY: *J281* Manish K.            */
/* REVISION: 8.6    LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan           */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00   BY: *N0KP* Mark Brown           */
/* $Revision: 1.4.1.2 $    BY: Jean Miller    DATE: 03/22/01 ECO: *P008*        */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* Use rcinvtbl.i to define temp table work_abs_mstr */

PROCEDURE chk_abs_inv_cons:
/*--------------------------------------------------------------------
  Purpose:    Determines whether two structures are compatible on an
              invoice.  These two structures would be exploded into their
              component SOs and the compatibility checked
  Parameters:
  Notes:      The two structures can be as follows:
              - two abs_mstr's (it could be a combination of a shipper
                and/or container in any order, both containers/shippers)
              - an abs_mstr and a sales order
                the absid and shipfrom (and NOT absid2 and shipfrom2)
                should be populated in that case.
---------------------------------------------------------------------*/
   define input parameter absid like abs_id no-undo.
   define input parameter shipfrom like abs_shipfrom no-undo.
   define input parameter absid2 like abs_id no-undo.
   define input parameter shipfrom2 like abs_shipfrom no-undo.
   define input parameter sonbr like so_nbr no-undo.
   define output parameter consolidate_ok as logical no-undo.

   define variable first_so like so_nbr no-undo.
   define variable l_msg_text as character no-undo.

   consolidate_ok = false.

   if sonbr <> "" then do:
      create work_abs_mstr.
      assign
         work_abs_order = sonbr.
   end.

   run bld_abs_wkfl
      (input absid,
       input shipfrom).

   if absid2 <> "" then
      run bld_abs_wkfl
         (input absid2,
          input shipfrom2).

   find first work_abs_mstr where work_abs_order <> ""
   no-lock no-error.

   if not available work_abs_mstr then
      consolidate_ok = true.
   else do:

      assign
         first_so = work_abs_order
         consolidate_ok = true.

      work_loop:
      for each work_abs_mstr no-lock
      break by work_abs_order:

         if first-of (work_abs_order) then do:

            if work_abs_order = "" then next.

            /* PROCEDURE FOR CONSOLIDATION RULES */
            {gprun.i ""soconso.p""
               "(input 1 ,
                 input first_so ,
                 input work_abs_order,
                 output consolidate_ok ,
                 output l_msg_text)"}

            if consolidate_ok = false then
               leave work_loop.

         end.

      end. /* for each work_abs_mstr */

   end.

   for each work_abs_mstr exclusive-lock:
      delete work_abs_mstr.
   end.

END PROCEDURE. /* procedure chk_abs_inv_cons */

PROCEDURE chk_abs_shp_cons:
/*--------------------------------------------------------------------
  Purpose:     Procedure determines whether the two structures when
               combined would result in more than one SO being referenced
               in the resulting structure.
  Parameters:
  Notes:       These two structures can be as follows:
               - two abs_mstr's (it could be a combination of a shipper and/or
                 container in any order, both containers/shippers)
               - an abs_mstr and a sales order
                 the absid and shipfrom (and NOT absid2 and shipfrom2)
                 should be populated in that case.
---------------------------------------------------------------------*/
   define input parameter absid like abs_id no-undo.
   define input parameter shipfrom like abs_shipfrom no-undo.
   define input parameter absid2 like abs_id no-undo.
   define input parameter shipfrom2 like abs_shipfrom no-undo.
   define input parameter sonbr like so_nbr no-undo.
   define output parameter consolidate_ok as logical no-undo.
   define output parameter other_so like so_nbr no-undo.

   define variable first_so like so_nbr no-undo.

   assign
      consolidate_ok = false
      other_so = ?.

   if sonbr <> "" then do:
      create work_abs_mstr.
      assign
         work_abs_order = sonbr.
      if recid(work_abs_mstr) = -1 then.
   end.

   run bld_abs_wkfl
      (input absid,
       input shipfrom).

   if absid2 <> "" then
      run bld_abs_wkfl
         (input absid2,
          input shipfrom2).

   /* Get some SO nbr to start search for any other SO */
   if sonbr = "" then
      find first work_abs_mstr where work_abs_order <> ""
      no-lock no-error.
   else
      find first work_abs_mstr where work_abs_order = sonbr
      no-lock no-error.

   if not available work_abs_mstr then
      consolidate_ok = true.

   else do:

      assign
         first_so = work_abs_order.

      find first work_abs_mstr where
                 work_abs_order <> first_so and
                 work_abs_order <> ""
      no-lock no-error.

      if available work_abs_mstr then
         assign
            consolidate_ok = false
            other_so = work_abs_order.
      else
         assign
            consolidate_ok = true.

   end.

   for each work_abs_mstr exclusive-lock:
      delete work_abs_mstr.
   end.

END PROCEDURE. /* procedure chk_abs_shp_cons */

PROCEDURE bld_abs_wkfl:
/*--------------------------------------------------------------------
  Purpose:     Explode the abs and its sub-structures recursively to
               populate the temb table with SOs that are referenced on
               the entire abs structure
  Parameters:
  Notes:
---------------------------------------------------------------------*/
   define input parameter absid like abs_id no-undo.
   define input parameter shipfrom like abs_shipfrom no-undo.

   define buffer abs_buff for abs_mstr.

   do for abs_buff:
      for each abs_buff where
               abs_shipfrom = shipfrom and
               abs_par_id = absid
      no-lock
      break by abs_shipfrom
            by abs_dataset
            by abs_order:

         if first-of (abs_order) and
            not can-find (first work_abs_mstr where
                                work_abs_order = abs_order)
         then do:

            create work_abs_mstr.
            assign
               work_abs_order = abs_order.

            if recid(work_abs_mstr) = -1 then.

         end.

         /* Recursive call, to find all the SOs referenced
          * in the child substructure */
         run bld_abs_wkfl
            (input abs_id,
             input abs_shipfrom).

      end. /* for each abs_mstr */
   end.

END PROCEDURE. /* procedure bld_abs_wkfl */

PROCEDURE get_abs_parent:
/*--------------------------------------------------------------------
  Purpose:     Used to determine abs_id of the structure that is the
               highest parent ("ancestor" for lack of a better term)
  Parameters:
  Notes:       par_absid of ? is returned in case of some abnormal situation
---------------------------------------------------------------------*/
   define input parameter absid like abs_id no-undo.
   define input parameter shipfrom like abs_shipfrom no-undo.
   define output parameter par_absid like abs_id no-undo.
   define output parameter par_shipfrom like abs_shipfrom no-undo.

   define buffer abs_buff for abs_mstr.

   define variable ctr as integer no-undo.

   assign
      par_absid = absid
      par_shipfrom = shipfrom.

   /* Go up the "chain" using abs_par_id till the "ancestor" is
    * determined. Prevent infinite looping due to freak data corruption */
   do while ctr < 1000:

      find abs_buff where
           abs_buff.abs_id = par_absid and
           abs_buff.abs_shipfrom = shipfrom
      no-lock no-error.

      /* The "ancestor" record has the abs_par_id = "" */
      if available abs_buff then do:
         if abs_buff.abs_par_id = "" then do:
            par_absid = abs_buff.abs_id.
            leave.
         end.
         else
            par_absid = abs_buff.abs_par_id.
      end.
      else do:
         par_absid = ?.
         leave.
      end.

   end.

   if ctr >= 1000 then
      par_absid = ?.

END PROCEDURE. /* procedure get_abs_parent */
